﻿using System;
using System.Data.Entity.Validation;
using System.Linq;
using System.ServiceProcess;
using System.Threading;
using Biz.EF.Gateway.Monitor;
using Biz.EF.Platform.SG;
using Biz.Process;
using Hjt212Parser;
using log4net;

namespace Biz
{
    public partial class BizProcessor : ServiceBase
    {
        private ILog log = LogManager.GetLogger(typeof(BizProcessor));
        int milliSeconds = 10;

        public BizProcessor()
        {
            InitializeComponent();
        }
        //test
        public void Main()
        {
            OnStart(null);
        }
        protected override void OnStart(string[] args)
        {
            //启动各路线程
            new Thread(ProcessData).Start();
            log.Info("各路线程启动完成...");
        }
        protected override void OnStop()
        {
            log.Info("The processor Stoped!");
            Environment.Exit(0);
        }
        private void ProcessData(object state)
        {
            while (true)
            {
                try
                {
                    using (var gCtx = new GatewayMonitorContext())
                    {
                        var flag = ProcessFlag.NotProcessed.ToString();
                        var items =
                            gCtx.received_msg.Where(t => t.is_length_right)
                                .Where(t => t.processed_flag.Equals(flag))
                                //.Where(t => t.cn.Equals("2061"))
                                .OrderBy(t => t.datatime)
                                .Take(1000)
                                .ToList();
                        foreach (var item in items)
                        {
                            log.Info(item.received_string);
                            switch (item.cn)
                            {
                                case "2011":
                                    Processor2011.Process(item.received_string);
                                    Processor2011ToMonitor.Process(item.received_string);
                                    Processor2011Alarm.Process(item.received_string);
                                    break;
                                case "2051":
                                    Processor2051.Process(item.received_string);
                                    Processor2051ToMonitor.Process(item.received_string);
                                    break;
                                case "2061":
                                    Processor2061.Process(item.received_string);
                                    Processor2061ToMonitor.Process(item.received_string);
                                    Processor2061SumToMonitorDay.Process(item.received_string);
                                    Processor2061SumToMonitorMonth.Process(item.received_string);
                                    Processor2061SumToMonitorYear.Process(item.received_string);
                                    break;
                                case "2031":
                                    Processor2031.Process(item.received_string);
                                    Processor2031ToOneTable.Process(item.received_string);
                                    Processor2031Sum2Month.Process(item.received_string);
                                    Processor2031Sum2Year.Process(item.received_string);
                                    break;
                                case "0000":
                                    ProcessorStatus.ProcessOffline(item.received_string);
                                    break;

                            }
                            item.processed_flag = ProcessFlag.ProcessingFinished.ToString();
                           
                           
                        }
                        gCtx.SaveChanges();
                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {

                           log.Error(string.Format("Class: {0}, Property: {1}, Error: {2}", validationErrors.Entry.Entity.GetType().FullName,
                                validationError.PropertyName,
                                validationError.ErrorMessage));
                        }
                    }
                   
                }
      
                catch (Exception ex)
                {
                    log.Error(ex);
                }
                finally
                {
                    ThreadUtil.Sleep(milliSeconds);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }
    }
}
